import 'dart:convert';
import 'package:http/http.dart' as http;
import 'package:netstorage/model/home_model.dart';
import 'package:netstorage/utils/navigator_utils.dart';

class HomeDao {
  // 请求首页数据
  static Future<HomeModel> fetch() async {
    String uri = 'https://xdfznh.club/zknoteb/flutter/homeData';
     // String uri = 'http://localhost:8086/zknoteb/flutter/homeData';
    var url = Uri.parse(uri);
    final res = await http.post(url);
    Utf8Decoder utf8decoder = const Utf8Decoder();
    String bodyStr = utf8decoder.convert(res.bodyBytes);
    if(res.statusCode == 200) {
      var map = jsonDecode(bodyStr);// 开发者后台响应来的响应结果，.data 是实际的数据
      return HomeModel.fromJson(map['data']);
    }
    if(res.statusCode == 401) {
      NavigatorUtils.goLogin();
      return HomeModel.empty();// 跳转到登录页面，后端响应来的数据已没有意义
    }
    throw Exception(bodyStr);
  }
}